﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TriganleTests.Question;


namespace TriganleTests.Answer
{
    class Jimson :ITriangle
    {
        private double GetArea(MyPoint A, MyPoint B, MyPoint C)
        {
            double area = 0;
            double c = Math.Pow(Math.Pow(A.X - B.X, 2.0) + Math.Pow(A.Y - B.Y, 2), 0.5);
            double b = Math.Pow(Math.Pow(A.X - C.X, 2.0) + Math.Pow(A.Y - C.Y, 2), 0.5);
            double a = Math.Pow(Math.Pow(C.X - B.X, 2.0) + Math.Pow(C.Y - B.Y, 2), 0.5);

            double p = (a + b + c) / 2.0;
            area = Math.Pow(p * (p - a) * (p - b) * (p - c), 0.5);
            return area;
        }

        public int InTriangle(MyPoint A, MyPoint B, MyPoint C, MyPoint test)
        {
            try
            {
                if (IsTrangle(A, B, C))
                {
                    double s1 = GetArea(A, B, test);
                    double s2 = GetArea(A, C, test);
                    double s3 = GetArea(C, B, test);
                    double s = GetArea(A, B, C);

                    if ((s1 + s2 + s3 - s) < 0.0000001)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                return -1;
            }
            catch
            { return -1; }
        }

        #region
        public int InTriangle1(MyPoint A, MyPoint B, MyPoint C, MyPoint test)
        {
            double x1, x2, x3, y1, y2, y3;
            int p1, p2, p3;
            int r1, r2, r3;

            if (IsTrangle(A, B, C))
            {
                GetFunctionFactor(A, B, C, out x1, out y1, out p1);
                GetFunctionFactor(A, C, B, out x2, out y2, out p2);
                GetFunctionFactor(B, C, A, out x3, out y3, out p3);

                GetFunctionFactor(A, B, test, out x1, out y1, out r1);
                GetFunctionFactor(A, C, test, out x2, out y2, out r2);
                GetFunctionFactor(B, C, test, out x3, out y3, out r3);

                if (p1 == r1 && p2 == r2 && p3 == r3)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            }

            return -1;
        }

        private bool IsTrangle(MyPoint A, MyPoint B, MyPoint C)
        {
            if ((A.X == B.X && B.X == C.X) || (A.Y == B.Y && B.Y == C.Y))
            {
                return false;
            }
            else
            {
                double ab = (A.Y - B.Y) / (A.X - B.X);
                double bc = (C.Y - B.Y) / (C.X - B.X);
                if (ab == bc)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        private void GetFunctionFactor(MyPoint A, MyPoint B, MyPoint test, out double x, out double y, out int position)
        {
            if (A.Y == B.Y)
            {
                x = 0.0;
                y = A.X;
            }
            else
            {
                if (A.X == B.X)
                {
                    x = 1;
                    y = A.X;
                }
                else
                {
                    x = (A.Y - B.Y) / (A.X - B.X);
                    y = A.Y - x * A.X;
                }
            }

            double pos = A.Y - A.X * x;
            position = 0;
            //  if (pos == y) position = 0;
            if (pos > y) position = 1;
            if (pos < y) position = -1;
        }
        #endregion
    }
}
